package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.service.OutputOrderService;
import com.ctgu.fastleader.vo.MoneyOfMonth;
import com.ctgu.fastleader.vo.OutputOrder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Resource
    OutputOrderService orderService;

    //加载所有商城订单
    @ResponseBody
    @RequestMapping("/allOutputOrder")
    public List<OutputOrder> getAllOrder(){
        return orderService.findAllOutputOrder();
    }

    //根据日期、订单号、商品号、顾客手机号搜索商城订单
    @ResponseBody
    @RequestMapping("/searchOutputOrderByKeyWord")
    public List<OutputOrder> searchOutputOrderByKeyWord(Integer orderid, Integer goodsid,String customerphone,Date createtime){
        return orderService.searchOutputOrderByKeyWord(orderid,goodsid,customerphone,createtime);
    }

    //新增商城订单
    @RequestMapping("/addOutputOrder")
    public String addOutputOrder(HttpServletRequest request) throws ParseException {
        int orderid = Integer.parseInt(request.getParameter("orderid"));
        int goodsid = Integer.parseInt(request.getParameter("goodsid"));
        int num = Integer.parseInt(request.getParameter("num"));
        int state = 0;
        double totalprice = 132;
        String customerphone = request.getParameter("customerphone");
        String customername = request.getParameter("customername");
        String customeradd = request.getParameter("customeradd");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date createtime = simpleDateFormat.parse(request.getParameter("createtime"));
        OutputOrder outputOrder = new OutputOrder(orderid,goodsid,num,customerphone,customername,customeradd,state,totalprice,createtime);
        orderService.insertSelective(outputOrder);
        return "redirect:/page/toOrder";
    }

    //删除商城订单通过id数组
    @ResponseBody
    @RequestMapping("/deleteOutputOrder")
    public int deleteOutputOrder(HttpServletRequest request,String[] obj){
        int[] id = new int[obj.length];
        for (String array:obj){
            orderService.deleteByPrimaryKey(Integer.parseInt(array));
        }
        return 0;
    }

//    首页待出库
    @ResponseBody
    @RequestMapping("/selectOutputOrderstate")
    public int selectOutputOrderstate(){
        return orderService.selectOutputOrderstate();
    }

    //    首页已发货
    @ResponseBody
    @RequestMapping("/selectOutputOrderstate2")
    public int selectOutputOrderstate2(){
        return orderService.selectOutputOrderstate2();
    }


    @ResponseBody
    @RequestMapping("/moneyEveryMonth")
    public List<MoneyOfMonth> moneyEveryMonth(){

        return orderService.moneyEveryMonth();
    }


//    首页条形图数据
    @ResponseBody
    @RequestMapping("/selectSaleOrder")
    public Integer[] selectSaleOrder(){
        List<Map<Integer,Integer>> list = orderService.selectSaleOrder();
        Integer[] value = new Integer[21];
        for(int j = 0;j<21;j++){
            value[j] = 0;
        }
        int k = 0;
        int n = 5;
        if(list.size() <= 5){
            n = list.size();
        }
        for(int i = 0;i<n;i++){
            try{
                Map<Integer,Integer> map = list.get(i);
                if(map.isEmpty()){
                    break;
                }else{
                    for(Map.Entry<Integer,Integer> entry : map.entrySet()){
                        String ss = String.valueOf(entry.getValue());
                        value[k++] = Integer.parseInt(ss);
                    }
                }
            }catch (Exception e){
               e.printStackTrace();
            }


        }
        value[20]=value[1]+value[3]+value[5]+value[7]+value[9];//总数
        if(value[20].equals(0)){

        }else{
            value[10]=value[1]*100/value[20];//第一个百分比
            value[11]=value[3]*100/value[20];//第二个百分比
            value[12]=value[5]*100/value[20];//第三个百分比
            value[13]=value[7]*100/value[20];//第四个百分比
            value[14]=value[9]*100/value[20];//第五个百分比
        }

//        System.out.println(value[10]);
        return value;
    }


//    配送管理所有待发货
    @ResponseBody
    @RequestMapping("/allOutputRecord")
    public List<OutputOrder> allOutputRecord(){
        return orderService.findAllOutputState1();
    }
}
